﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.DataTransfer;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Notifications;
using Windows.UI.StartScreen;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace TileAndToastDemo
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Windows.ApplicationModel.DataTransfer.DataTransferManager.GetForCurrentView().DataRequested += MainPage_DataRequested;
        }

        async void MainPage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var _Deferral = args.Request.GetDeferral();

            // fetch data
            var _Data = await GetData();
            if (_Data == null)
            {
                // fail
                args.Request.FailWithDisplayText("No data to share");
            }
            else
            {
                // share
                var _String = string.Format("<h1>Hello {0}</h1>", _Data);
                var _Html = HtmlFormatHelper.CreateHtmlFormat(_String);
                args.Request.Data.SetHtmlFormat(_Html);
                args.Request.Data.Properties.Title = "My Data";
                args.Request.Data.Properties.Description = "Data from App";
            }

            _Deferral.Complete();
        }

        async Task<string> GetData() { return "Fake Data"; }

        private async void AddSecondaryTile(string id, string text)
        {
            SecondaryTile _SecondaryTile = new SecondaryTile("Views.DetailsView",
                                                            text,
                                                            id,
                                                            new Uri("ms-appx:///Assets/square150x150Tile.png"),
                                                            TileSize.Square150x150);

            _SecondaryTile.VisualElements.Wide310x150Logo = new Uri("ms-appx:///Assets/wide310x150Tile.png");
            _SecondaryTile.VisualElements.Square310x310Logo = new Uri("ms-appx:///Assets/square310x310Tile.png");
            // note: if ommited, system creates by scaling down from 310x310
            _SecondaryTile.VisualElements.Square70x70Logo = new Uri("ms-appx:///Assets/square70x70Tile.png"); 
            // inherited from the parent application tile by default. 
            _SecondaryTile.VisualElements.Square30x30Logo = new Uri("ms-appx:///Assets/square30x30Tile.png");
            _SecondaryTile.VisualElements.ShowNameOnSquare150x150Logo = true;
            _SecondaryTile.VisualElements.ShowNameOnWide310x150Logo = true;
            _SecondaryTile.VisualElements.ShowNameOnSquare310x310Logo = true;
            _SecondaryTile.VisualElements.ForegroundText = ForegroundText.Light;
            _SecondaryTile.RoamingEnabled = false; 
            await _SecondaryTile.RequestCreateAsync();
        }

        private async void UpdateSecondaryTileText(string id, string text)
        {
            var _Tiles = await Windows.UI.StartScreen.SecondaryTile.FindAllAsync();
            var _SecondaryTile = _Tiles
                .Where(x => x.TileId == "Views.DetailsView")
                .FirstOrDefault(x => x.Arguments == id);
            if (_SecondaryTile == null)
                return;
            _SecondaryTile.DisplayName = text;
            await _SecondaryTile.UpdateAsync();
        }

        private void UpdateSecondaryTileBadge(string id, int badge)
        {
            var _Type = BadgeTemplateType.BadgeNumber;
            var _Tile = BadgeUpdateManager.GetTemplateContent(_Type);

            var _XmlBadge = (_Tile.SelectSingleNode("/badge") as XmlElement);
            _XmlBadge.SetAttribute("value", badge.ToString());

            var _Updater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
            _Updater.Clear();

            var _Notification = new BadgeNotification(_Tile);
            _Updater.Update(_Notification);
        }

        private void UpdatePrimaryTileNotifications(string image, string title, string text)
        {
            var _Type = TileTemplateType.TileSquare150x150Text02;
            var _Tile = TileUpdateManager.GetTemplateContent(_Type);

            var _ImageXml = (_Tile.GetElementsByTagName("image")[0] as XmlElement);
            _ImageXml.SetAttribute("src", image);

            var _TitleXml = _Tile.GetElementsByTagName("text")[0];
            _TitleXml.InnerText = title;

            var _TextXml = _Tile.GetElementsByTagName("text")[1];
            _TextXml.InnerText = text;

            var _Updater = TileUpdateManager.CreateTileUpdaterForApplication();
            _Updater.Clear();

            var _Notification = new TileNotification(_Tile) { Tag = "no-repeat" };
            _Updater.Update(_Notification);
        }

        void UpdateMediumTile(string image, string text1)
        {
            var _Type = Windows.UI.Notifications.TileTemplateType.TileSquare150x150PeekImageAndText01;
            var _Tile = Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(_Type);

            (_Tile.GetElementsByTagName("image")[0] as XmlElement).SetAttribute("src", image);
            _Tile.GetElementsByTagName("text")[0].InnerText = text1;

            var _Notification = new Windows.UI.Notifications.TileNotification(_Tile) { Tag = "no-repeat" };
            var _Updater = Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication();
            _Updater.Clear();
            _Updater.Update(_Notification);
        }



        void ShowToast(string image, string title, string text)
        {
            var _Type = ToastTemplateType.ToastImageAndText02;
            var _ToastTemplate = ToastNotificationManager.GetTemplateContent(_Type);

            var _ImageXml = (_ToastTemplate.GetElementsByTagName("image")[0] as XmlElement);
            _ImageXml.SetAttribute("src", image);

            var _TitleXml = _ToastTemplate.GetElementsByTagName("text")[0];
            _TitleXml.InnerText = title;

            var _TextXml = _ToastTemplate.GetElementsByTagName("text")[1];
            _TextXml.InnerText = text;

            var _Toast = new ToastNotification(_ToastTemplate);
            ToastNotificationManager.CreateToastNotifier().Show(_Toast);
        }

        private void CreateTileClick(object sender, RoutedEventArgs e)
        {
            AddSecondaryTile("21", "Sample Text");
        }

        private void UpdateTileClick(object sender, RoutedEventArgs e)
        {
            UpdateMediumTile("ms-appx:///Assets/MVAsquare150x150.png", "Image?");
        }

        private void ShowToastClick(object sender, RoutedEventArgs e)
        {
            ShowToast("ms-appx:///Assets/MVAsquare150x150.png", "Toast Demo", "Here's the toast!");
        }
    }
}

